<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Order extends BaseController
{
    public function order_list()
    {
        $order_sn = Request::get('order_sn','','trim,htmlspecialchars,strip_tags');
        $order_amount_min = Request::get('order_amount_min','','trim,htmlspecialchars,strip_tags');
        $order_amount_max = Request::get('order_amount_max','','trim,htmlspecialchars,strip_tags');
        $status_id = Request::get('status_id',[],'trim,htmlspecialchars,strip_tags');

        //$data=Request::get('','','trim,htmlspecialchars,strip_tags');
        $data=[
          'order_sn'=>$order_sn,
          'order_amount_min'=>$order_amount_min,
          'order_amount_max'=>$order_amount_max,
            'status_id'=>$status_id,
        ];



        $dd=\app\index\model\Order::getOrder($data,$data);

        $status=\app\index\model\OrderStatus::getStatus();









//        echo "<pre>";
//        print_r($zt);
//        echo "</pre>";
//        exit;

        $empty="<tr><td colspan='8'>暂无数据</td></tr>";
        View::assign('empty',$empty);
        View::assign('data',$data);
        View::assign('status_id',$status_id);
        return View::fetch("",['dd'=>$dd,'status'=>$status]);
    }

    //软删除
    public function del(){
        $id =Request::get('id','','');
        try{
            \app\index\model\Order::destroy($id);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('删除成功!',getenv("HTTP_REFERER"));

    }
    //展示回收站
    public function order_recycle()
    {
        $db= Db::table('ecm_order')->where('del','>',0)
            ->select()
            ->toArray();


        $empty="<tr><td colspan='8'>暂无数据</td></tr>";

        View::assign('empty',$empty);
        return View::fetch('',['db'=>$db]);
    }
    //回收数据
    public function recycle(){
        $id =Request::get('id','','');

        try{
            $user = \app\index\model\Order::onlyTrashed()->find($id);
            $user->restore();
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('恢复成功！',url('order_list'));

    }

    //物理删除
    public function real_del(){
        $id =Request::get('id','','');

        Db::startTrans();
        try{
            if(strpos($id,',')>0){
                $id=explode(',',$id);
                foreach ($id as $v){
                    \app\index\model\Order::destroy($v,true);
                }
            }else {
                \app\index\model\Order::destroy($id, true);

            }


            //提交事务
            Db::commit();

        }catch (\Exception $e){
            $this->error($e->getMessage());
            // 回滚事务
            Db::rollback();;
        }
        $this->success('删除成功!',getenv("HTTP_REFERER"));
    }

    //多删
    public function all_del(){
        $id =Request::get('id',"",'');

        Db::startTrans();

        try{
            \app\index\model\Order::destroy($id);

            //提交事务
            Db::commit();

        }catch (\Exception $e){
            $this->error($e->getMessage());
            // 回滚事务
            Db::rollback();
        }
        $this->success('删除成功!',getenv("HTTP_REFERER"));

    }


}
